package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 从MySQL中查询 数据同步到redis数据库中
 *
 * redis数据预热
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
//spring容器启动  并且初始化完成 之后  马上执行操作
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("-------------------vo-------------------------");

        //mysql -- redis  -- vo
        List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail detail:list){
            //如果vo已经存在了  跳过  不需要再进行初始化  防止怇被覆盖
            if (strategyStatisRedisService.isStrategyVOExist(detail.getId())){
                continue;  //跳过
            }



            StrategyStatisVO vo = new StrategyStatisVO();
            BeanUtils.copyProperties(detail,vo);//同属姓名可以拷贝
            //目的地 id 明细
            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setStrategyId(detail.getId());
            strategyStatisRedisService.setStrategyStatisVo(vo);
        }

    }
}
